<?php

/***** Blog information *****/
function bloginfo($show='',$is_echo=true) {
    $info = get_bloginfo($show);
    if($is_echo)
    	echo apply_filters($info);
    else
    	return apply_filters($info);
}

function get_bloginfo($show='') {

	switch($show) {
	case 'url' :
	case 'home' : 
		$output = get_settings('home');
		break;
	case 'siteurl' :
		$output = get_settings('siteurl');
		break;
	case 'stylesheet_url':
		$output = get_stylesheet_uri();
		break;
	case 'stylesheet_directory':
		$output = get_stylesheet_directory_uri();
		break;
	case 'template_directory':
	case 'template_url':
		$output = get_template_directory_uri();
		break;
	case 'admin_email':
		$output = get_settings('admin_email');
		break;
	case 'charset':
		$output = get_settings('blog_charset');
		if ('' == $output) $output = 'gb2312';
		break;
	case 'name':
	default:
		$output = get_settings('blogname');
		break;
	}
	return $output;
}

/***** StyleSheet functions *****/
function get_stylesheet_uri() {
	$stylesheet_dir_uri = get_stylesheet_directory_uri();
	$stylesheet_uri = $stylesheet_dir_uri . "/style.css";
	return $stylesheet_uri;
}

function get_stylesheet_directory_uri() {
	$stylesheet = get_stylesheet();
	$stylesheet_dir_uri = get_theme_root_uri() . "/$stylesheet";
	return $stylesheet_dir_uri;
}

function get_stylesheet() {
	return get_settings('stylesheet');
}

function get_theme_root_uri() {
	return (get_settings('siteurl') . "/tr-themes");
}

/***** tr-themes functions *****/

function the_post_title($echo = true) {
    global $post;
	$the_post_title = $post->post_title;
	if ($echo)
    	echo $the_post_title;
	else
		return $the_post_title;
}

function the_post_url($echo = true) {
    global $post;
	$the_post_url = $post->post_url;
	if ($echo)
    	echo $the_post_url;
	else
		return $the_post_url;
}

function the_content($echo = true) {
    global $post;
	$the_content = $post->post_content;
	if(!user_can_readfullpost())
		$the_content = substr_for_gb2312($the_content,0,400);
	if ($echo)
    	echo $the_content;
	else
		return $the_content;
}

function the_content_abs($echo = true) {
    global $post;
	$the_content = $post->post_content;
	$the_content = substr_for_gb2312($the_content,0,200);
	if ($echo)
    	echo $the_content;
	else
		return $the_content;
}


function the_post_author($echo = true) {
	global $post;
	$the_author_id = $post->post_author;
	$the_post_author = get_name_by_id($the_author_id,false);
	if ($echo)
    	echo $the_post_author;
	else
		return $the_post_author;
}

function the_siteurl($echo = true) {
	$the_siteurl = get_option('siteurl');
	if ($echo)
    	echo $the_siteurl;
	else
		return $the_siteurl;
}

function the_post_author_id($echo = true) {
    global $post;
	$the_post_author_id = $post->post_author;
	if ($echo)
    	echo $the_post_author_id;
	else
		return $the_post_author_id;
}

function the_post_user_id($echo = true) {
    global $post;
	$the_post_user_id = $post->user_id;
	if ($echo)
    	echo $the_post_user_id;
	else
		return $the_post_user_id;
}

function the_post_id($echo = true) {
    global $post;
	$the_post_id = $post->ID;
	if ($echo)
    	echo $the_post_id;
	else
		return $the_post_id;
}

function get_post_tags($is_url=true){
	
	global $trdb;
	$cuurent_postID = the_post_id(false);
	$cuurent_postID = $trdb->escape($cuurent_postID);
	
	$request = "SELECT tag_name,tag_nicename FROM $trdb->tags LEFT JOIN $trdb->post2tag ON $trdb->tags.tag_id = $trdb->post2tag.tag_id WHERE $trdb->post2tag.post_id = $cuurent_postID";
	
	$tags = $trdb->get_results($request);
	$numbers = count($tags);
	
	for($i=0;$i<$numbers;$i++)
	{
		$tag = $tags[$i];
		$tag_name = $tag->tag_name;
		$tag_nicename = $tag->tag_nicename;
		if($is_url)
			echo " <A HREF='?place=$tag_name' title='$tag_nicename'>$tag_name</A>";
		else
			echo "$tag_name ";
	}
}

function get_post_tags_5($is_url=true){
	
	global $trdb;
	$cuurent_postID = the_post_id(false);
	$cuurent_postID = $trdb->escape($cuurent_postID);
	
	$request = "SELECT tag_name,tag_nicename FROM $trdb->tags LEFT JOIN $trdb->post2tag ON $trdb->tags.tag_id = $trdb->post2tag.tag_id WHERE $trdb->post2tag.post_id = $cuurent_postID";
	
	$tags = $trdb->get_results($request);
	$numbers = count($tags);

	if($numbers>5)
		$number=5;
	else
		$number=$numbers;
	
	for($i=0;$i<$number;$i++)
	{
		$tag = $tags[$i];
		$tag_name = $tag->tag_name;
		$tag_nicename = $tag->tag_nicename;
		if($is_url)
			echo " <A HREF='?place=$tag_name' title='$tag_nicename'>$tag_name</A>";
		else
			echo "$tag_name ";
	}
	if($numbers>5)
		echo " ...";
}


function the_favor_count($echo = true) {
    global $post;
	$the_favor_count = $post->favor_count;
	if ($echo)
    	echo $the_favor_count;
	else
		return $the_favor_count;
}

function the_shade_id($echo = true) {
    global $post;
	$the_favor_count = $post->favor_count;
	
	$favor_count_level2 = get_option('favor_count_level2');
	$favor_count_level3 = get_option('favor_count_level3');
	
	if($the_favor_count<$favor_count_level2)
		$the_shade_id = 1;
	elseif ($the_favor_count>=$favor_count_level2&&$the_favor_count<$favor_count_level3)
		$the_shade_id = 2;
	else
		$the_shade_id = 3;
	if ($echo)
    	echo $the_shade_id;
	else
		return $the_shade_id;
}

function get_viaurls() {
	$url1 = the_post_url(false);
	$url_display = substr_for_gb2312($url1,0,25);
	$result = "(链接 <A href=\"$url1\" target=_blank>$url_display</A>)";
	echo $result;
}

function get_added_urls_inputbox() {
	global $trdb;
	$cuurent_postID = the_post_id(false);
	
	$request = "SELECT post_url FROM $trdb->postaddurl WHERE post_id=$cuurent_postID";
	$urls = $trdb->get_results($request);
	$result = "";
	
	for($i=0;$i<count($urls);$i++)
	{
		$url = $urls[$i]->post_url;
		$url_count = $i+1;
		$result .= "<input type=\"text\" name=submiturl".$url_count." id=submiturl".$url_count." value=\"".$url."\" class=\"form-full\" />";
		$result .= " <SPAN id=removeurl".$url_count." class=cursor_pointer onclick=\"javascript:xajax_removeUrlInput($url_count);\"><IMG border=0 align=absmiddle  src='".bloginfo('stylesheet_directory',false)."/img/remove-small.gif'/></SPAN>";
	}
	
	echo $result;
}


function get_added_urls_number($echo=true) {
	global $trdb;
	$cuurent_postID = the_post_id(false);
	
	$request = "SELECT count(rel_id) FROM $trdb->postaddurl WHERE post_id=$cuurent_postID";
	$result = $trdb->get_var($request);
	
	if($echo)
		echo $result;
	else
		return $result;
}

function get_z_index(){
	global $db_query;
	$current_no = $db_query->current_post;
	echo (1000-5*$current_no);
}

/***** comment function ******/
function the_comments() {
	global $trdb;

	$cuurent_postID = the_post_id(false);
	$request = "SELECT * FROM $trdb->comments WHERE comment_post_id=$cuurent_postID AND comment_approved = '1' AND comment_type = 'trip'";

	$comments = $trdb->get_results($request);
	$comments_count = count($comments);
	
	$comment_li = "";
	
	for($i=0;$i<$comments_count;$i++){
		
		$comment = $comments[$i];
		$comment_id = $comment->comment_ID;
		$comment_post_id = $comment->comment_post_id;
		$comment_user_id = $comment->comment_user_id;
		$comment_date = $comment->comment_date;
		$comment_content = $comment->comment_content;
		$comment_content = make_clickable($comment_content);
		
		$comment_date_past = get_past_time($comment_date);
		$the_comment_author = get_name_by_id($comment_user_id,false);
		$comment_author_url = get_option('siteurl')."/?user=".$the_comment_author;
		
		$comment_li .= "<span class=\"c-normal\" id=\"c$comment_id\">by <strong><a href=\"$comment_author_url\">$the_comment_author</a></strong>";
		$comment_li .= " <span class=viaurl><a href=\"#c$comment_id\">$comment_date_past</a></span> ";
		
		if (user_can_hide_post()) {
			$comment_li .= "";
			$comment_li .= "<span class=cursor_pointer id=\"colorthumbs$comment_id\" onclick=\"javascript(buryComment($comment_id))\"><img src=\"".bloginfo('stylesheet_directory',false)."/img/c-bury-off.png\" width=\"14\" height=\"14\" alt=\"bury this\" />";
			$comment_li .= "</span>";
		}
		
		//$comment_li .= "<div class=\"c-digg\" id=\"c-digg-1896227\"><span id=\"cfdiggs1896227\" ></span><strong id=\"strong-1896227\">+ 0 diggs</strong></div>";
		$comment_li .= "<br />";
		$comment_li .= "$comment_content<br /><br /></span>";
	}
	
	echo $comment_li;
}

function get_comment_number($post_id,$post_type){
	global $trdb;
	$request = "SELECT count(comment_id) FROM $trdb->comments WHERE comment_post_id = $post_id AND comment_approved = '1' AND comment_type='$post_type'";
	$comment_count = $trdb->get_var($request);
	return $comment_count;
}

/***** user functions *****/
function the_user_id($echo = true) {
    global $user;
	$the_user_id = $user->ID;
	if ($echo)
    	echo $the_user_id;
	else
		return $the_user_id;
}

function the_user_name($echo = true) {
    global $user;
	$the_user_name = $user->user_login;
	if ($echo)
    	echo $the_user_name;
	else
		return $the_user_name;
}

function the_user_email($echo = true) {
    global $user;
	$the_user_email = $user->user_email;
	if ($echo)
    	echo $the_user_email;
	else
		return $the_user_email;
}

function the_user_blog($echo = true) {
    global $user;
	$the_user_blog = $user->user_blog;
	if ($echo)
    	echo $the_user_blog;
	else
		return $the_user_blog;
}

function the_user_submitted_number($echo = true) {
	global $trdb;
    $user_id = the_user_id(false);
    
    $request = "SELECT count(ID) FROM $trdb->posts WHERE post_author = $user_id AND post_status = 'publish'";
    $the_user_submitted_number = $trdb->get_var($request);
    
	if ($echo)
    	echo $the_user_submitted_number;
	else
		return $the_user_submitted_number;
}

function the_user_submitted_to_home_number($echo = true) {
	global $trdb;
    $user_id = the_user_id(false);
    
    $request = "SELECT count(ID) FROM $trdb->posts WHERE post_author = $user_id AND post_status = 'publish' AND post_favor = 'yes'";
    $the_user_submitted_to_home_number = $trdb->get_var($request);
    
	if ($echo)
    	echo $the_user_submitted_to_home_number;
	else
		return $the_user_submitted_to_home_number;
}

function the_user_favorited_number($echo = true) {
	global $trdb;
	$user_id = the_user_id(false);
	
	$request = "SELECT count(rel_id) FROM $trdb->post2user WHERE user_id = $user_id";
	$the_user_favorited_number = $trdb->get_var($request);
	
	if ($echo)
    	echo $the_user_favorited_number;
	else
		return $the_user_favorited_number;
}

function the_user_commented_number($echo = true) {
	global $trdb;
	$user_id = the_user_id(false);
	
	$request = "SELECT count(comment_ID) FROM $trdb->comments WHERE comment_user_id = $user_id AND comment_type='trip'";
	$the_user_commented_number = $trdb->get_var($request);
	
	if ($echo)
    	echo $the_user_commented_number;
	else
		return $the_user_commented_number;
}

function user_post_homepage_ratio($echo=true) {
	global $trdb;
	
	$total_posts = the_user_submitted_number(false);
	$home_posts = the_user_submitted_to_home_number(false);
	
	if($total_posts!=0)
		$ratio = (int)($home_posts*100/$total_posts);
	else 
		$ratio = 0;
	
	if ($echo)
    	echo $ratio;
	else
		return $ratio;
}

function the_userplan_number($echo = true) {
	global $trdb;
	$user_id = the_user_id(false);
	
	$request = "SELECT count(ID) FROM $trdb->plans WHERE user_id = $user_id";
	$the_userplan_number = $trdb->get_var($request);
	
	if ($echo)
    	echo $the_userplan_number;
	else
		return $the_userplan_number;
}

function the_userplan_favorited_number($echo = true) {
	global $trdb;
	$user_id = the_user_id(false);
	
	$request = "SELECT count(rel_id) FROM $trdb->plan2user WHERE user_id = $user_id";
	$the_userplan_favorited_number = $trdb->get_var($request);
	
	if ($echo)
    	echo $the_userplan_favorited_number;
	else
		return $the_userplan_favorited_number;
}


function the_userplan_commented_number($echo = true) {
	global $trdb;
	$user_id = the_user_id(false);
	
	$request = "SELECT count(comment_ID) FROM $trdb->comments WHERE comment_user_id = $user_id AND comment_type='plan'";
	$the_userplan_commented_number = $trdb->get_var($request);
	
	if ($echo)
    	echo $the_userplan_commented_number;
	else
		return $the_userplan_commented_number;
}

function subnav2_userhome_text() {
	if(is_user_home())
		echo "<SPAN> 收藏的文章</SPAN>";
	else 
		echo "<A href=\"./?user=".the_user_name(false)."\"> 收藏的文章</A>";
}

function subnav2_usersubmitted_text() {
	if(is_user_submitted())
		echo "<SPAN> 提交的文章</SPAN>";
	else 
		echo "<A href=\"./?user=".the_user_name(false)."&submitted\"> 提交的文章</A>";
}

function subnav2_usercommented_text() {
	if(is_user_commented())
		echo "<SPAN> 评论的文章</SPAN>";
	else 
		echo "<A href=\"./?user=".the_user_name(false)."&commented\"> 评论的文章</A>";
}

function get_tags(){
	global $trdb, $db_query;
	$url_param = "";

	if(isset($db_query->query_vars['user'])){
		$user_name = $db_query->query_vars['user'];
		if(is_username_exist($user_name)){
			if(isset($db_query->query_vars['submitted'])){
				$url_param = "&user=$user_name&submitted";
				$request = "SELECT a.tag_name,a.tag_nicename,COUNT(a.tag_name) AS title FROM $trdb->tags a";
				$request .= " INNER JOIN $trdb->post2tag b ON a.tag_ID = b.tag_id";
				$request .= " INNER JOIN $trdb->posts c ON c.ID = b.post_id";
				$request .= " INNER JOIN $trdb->users d ON c.post_author = d.ID AND d.user_login='$user_name'";
				$request .= " WHERE c.post_status='publish'";
				$request .= " GROUP BY a.tag_name";
			}
			elseif(isset($db_query->query_vars['commented'])){
				$url_param = "&user=$user_name&commented";
				$request = "SELECT a.tag_name,a.tag_nicename,COUNT(a.tag_name) AS title FROM $trdb->tags a";
				$request .= " INNER JOIN $trdb->post2tag b ON a.tag_ID = b.tag_id";
				$request .= " INNER JOIN $trdb->comments c ON c.comment_post_id = b.post_id AND c.comment_type='trip'";
				$request .= " INNER JOIN $trdb->users d ON c.comment_user_id = d.ID AND d.user_login='$user_name'";
				$request .= " GROUP BY a.tag_name";
			}
			else{
				$url_param = "&user=$user_name";
				$request = "SELECT a.tag_name,a.tag_nicename,COUNT(a.tag_name) AS title FROM $trdb->tags a";
				$request .= " INNER JOIN $trdb->post2tag b ON a.tag_ID = b.tag_id";
				$request .= " INNER JOIN $trdb->post2user c ON c.post_id = b.post_id";
				$request .= " INNER JOIN $trdb->users d ON c.user_id = d.ID AND d.user_login='$user_name'";
				$request .= " GROUP BY a.tag_name";
			}
		}
		else
			$request = "SELECT * FROM $trdb->tags WHERE 1=2";
	}
	if(isset($db_query->query_vars['userplan'])){
		$user_name = $db_query->query_vars['userplan'];
		if(is_username_exist($user_name)){
			if(isset($db_query->query_vars['favorited'])){
				$url_param = "&userplan=$user_name&favorited";
				$request = "SELECT a.tag_name,a.tag_nicename,COUNT(a.tag_name) AS title FROM $trdb->tags a";
				$request .= " INNER JOIN $trdb->places b ON a.tag_ID = b.tag_id";
				$request .= " INNER JOIN $trdb->plan2user c ON c.plan_id = b.plan_id";
				$request .= " INNER JOIN $trdb->users d ON c.user_id = d.ID AND d.user_login='$user_name'";
				$request .= " GROUP BY a.tag_name";
			}
			elseif(isset($db_query->query_vars['commented'])){
				$url_param = "&userplan=$user_name&commented";
				$request = "SELECT a.tag_name,a.tag_nicename,COUNT(a.tag_name) AS title FROM $trdb->tags a";
				$request .= " INNER JOIN $trdb->places b ON a.tag_ID = b.tag_id";
				$request .= " INNER JOIN $trdb->comments c ON c.comment_post_id = b.plan_id AND c.comment_type='plan'";
				$request .= " INNER JOIN $trdb->users d ON c.comment_user_id = d.ID AND d.user_login='$user_name'";
				$request .= " GROUP BY a.tag_name";
			}
			else{
				$url_param = "&userplan=$user_name";
				$request = "SELECT a.tag_name,a.tag_nicename,COUNT(a.tag_name) AS title FROM $trdb->tags a";
				$request .= " INNER JOIN $trdb->places b ON a.tag_ID = b.tag_id";
				$request .= " INNER JOIN $trdb->plans c ON c.ID = b.plan_id";
				$request .= " INNER JOIN $trdb->users d ON c.user_id = d.ID AND d.user_login='$user_name'";
				$request .= " GROUP BY a.tag_name";
			}
		}
		else
			$request = "SELECT * FROM $trdb->tags WHERE 1=2";
	}
	else
		$request = "SELECT a.tag_name,a.tag_nicename,count(b.post_id) AS title FROM $trdb->tags a, $trdb->post2tag b,$trdb->posts c WHERE a.tag_id=b.tag_id AND b.post_id=c.ID AND c.post_status='publish' GROUP BY b.tag_id";
	
	$tags = $trdb->get_results($request);
	$numbers = count($tags);
	
	for($i=0;$i<$numbers;$i++)
	{
		$tag = $tags[$i];
		$tag_name = $tag->tag_name;
		$tag_nicename = $tag->tag_nicename;
		
		$title = $tag->title;
		if($title<=1)
			$level=0;
		elseif($title<=2)
			$level=1;
		elseif ($title<=4)
			$level=2;
		elseif ($title<=8)
			$level=3;
		elseif ($title<=16)
			$level=4;
		elseif ($title<=32)
			$level=5;
		elseif ($title<=54)
			$level=6;
		elseif ($title<=81)
			$level=7;
		elseif ($title<=128)
			$level=8;
		elseif ($title<=256)
			$level=9;
		else
			$level=10;
		
		echo " <SPAN class=level$level><A href=\"./?place=$tag_name".$url_param."\" title='$tag_nicename'>$tag_name</A></SPAN>";
	}
}

function get_totle_post($echo = true) {
    global $trdb;
    
    $request = "SELECT COUNT(ID) FROM $trdb->posts WHERE post_status = 'publish'";
    $totle_post = $trdb->get_var($request);
    
	if ($echo)
    	echo $totle_post;
	else
		return $totle_post;
}

function get_totle_user($echo = true) {
    global $trdb;
    
    $request = "SELECT COUNT(ID) FROM $trdb->users";
    $totle_user = $trdb->get_var($request);
    
	if ($echo)
    	echo $totle_user;
	else
		return $totle_user;
}

function get_user_list() {
    global $trdb,$db_query;

	$result="";
    
	if(isset($db_query->query_vars['p']))
		$p = $db_query->query_vars['p'] - 1;
	else
		$p=0;

	$page_post_num = get_option('userlist_count');
	$begin_num = $p*$page_post_num;

    $request = "SELECT a.ID,a.user_login,COUNT(b.ID) AS user_posts FROM $trdb->users a LEFT JOIN $trdb->posts b ON a.ID = b.post_author AND b.post_status = 'publish'";
	$request .= " GROUP BY a.ID";
	$request .= " ORDER BY user_posts DESC, a.user_registered";
	$request .= " LIMIT $begin_num,$page_post_num";
    $users = $trdb->get_results($request);

	for($i=0;$i<count($users);$i++)
	{
		$user = $users[$i];
		$result .= "<tr class=\"withover\"><td class=\"count\"><span>".($i+1+$begin_num)."<span></td><td>&nbsp;</td>";
		$result .= "<td><a href=\"./?user=$user->user_login&profile\">$user->user_login</a></td>";
		$result .= "<td class=\"short\"><a href=\"./?user=$user->user_login&submitted\">".user_submitted_number($user->ID)."</a></td>";
		$result .= "<td class=\"short\"><a href=\"./?user=$user->user_login\">".user_favorited_number($user->ID)."</a></td>";
		$result .= "<td class=\"short\"><a href=\"./?user=$user->user_login&commented\">".user_commented_number($user->ID)."</a></td>";
	}

    echo $result;
}

function user_submitted_number($user_id) {
	global $trdb;
    
    $request = "SELECT count(ID) FROM $trdb->posts WHERE post_author = $user_id AND post_status = 'publish'";
    $user_submitted_number = $trdb->get_var($request);
    
	return $user_submitted_number;
}

function user_submittedplan_number($user_id) {
	global $trdb;
    
    $request = "SELECT count(ID) FROM $trdb->plans WHERE user_id = $user_id AND post_status = 'publish'";
    $user_submitted_number = $trdb->get_var($request);
    
	return $user_submitted_number;
}

function user_submitted_to_home_number($user_id) {
	global $trdb;
    
    $request = "SELECT count(ID) FROM $trdb->posts WHERE post_author = $user_id AND post_status = 'publish' AND post_favor = 'yes'";
    $user_submitted_to_home_number = $trdb->get_var($request);
    
	return $user_submitted_to_home_number;
}

function user_favorited_number($user_id) {
	global $trdb;
	
	$request = "SELECT count(rel_id) FROM $trdb->post2user WHERE user_id = $user_id";
	$user_favorited_number = $trdb->get_var($request);
	
	return $user_favorited_number;
}

function user_favoritedplan_number($user_id) {
	global $trdb;
	
	$request = "SELECT count(rel_id) FROM $trdb->plan2user WHERE user_id = $user_id";
	$user_favorited_number = $trdb->get_var($request);
	
	return $user_favorited_number;
}

function user_commented_number($user_id) {
	global $trdb;
	
	$request = "SELECT count(comment_ID) FROM $trdb->comments WHERE comment_user_id = $user_id AND comment_type='trip'";
	$user_commented_number = $trdb->get_var($request);
	
	return $user_commented_number;
}

function user_commentedplan_number($user_id) {
	global $trdb;
	
	$request = "SELECT count(comment_ID) FROM $trdb->comments WHERE comment_user_id = $user_id AND comment_type='plan'";
	$user_commented_number = $trdb->get_var($request);
	
	return $user_commented_number;
}

function post_homepage_ratio($user_id) {
	global $trdb;
	
	$total_posts = user_submitted_number($user_id);
	$home_posts = user_submitted_to_home_number($user_id);
	
	if($total_posts!=0)
		$ratio = (int)($home_posts*100/$total_posts);
	else 
		$ratio = 0;
	
	return $ratio;
}

function get_hot_tags($echo=true){
	global $trdb, $db_query;
	$request = "SELECT a.tag_name,a.tag_nicename,count(b.post_id) AS title FROM $trdb->tags a, $trdb->post2tag b,$trdb->posts c WHERE a.tag_id=b.tag_id AND b.post_id=c.ID AND c.post_status='publish' GROUP BY b.tag_id ORDER BY title DESC LIMIT 10";
	$sText = "";
	
	$tags = $trdb->get_results($request);
	$numbers = count($tags);
	
	for($i=0;$i<$numbers;$i++)
	{
		$tag = $tags[$i];
		$tag_name = $tag->tag_name;
		$tag_nicename = $tag->tag_nicename;
		
		$sText .= " <A href=\"./?place=".urlencode($tag_name)."\">$tag_name</A>";
	}

	if($echo)
		echo $sText;
	else
		return $sText;
}

function get_relate_tags($org_tagid,$echo=true,$plan=true){
	global $trdb, $db_query;
	$sText = "";

	if($org_tagid=='')
		$org_tagid=0;

	// 大理下面的"三塔"
	$request1 = "SELECT tag_name,tag_nicename FROM $trdb->tags WHERE tag_parent=$org_tagid LIMIT 5";
	$tags = $trdb->get_results($request1);
	$numbers = count($tags);
	for($i=0;$i<$numbers;$i++)
	{
		$tag = $tags[$i];
		$tag_name = $tag->tag_name;
		$tag_nicename = $tag->tag_nicename;
	
		if($plan)
			$sText .= " <A href=\"javascript://\" onclick=\"javascript:searchBarNewAdd('$tag_name')\">$tag_name</A>";
		else{
			$sText .= " <A href=\"./?place=".urlencode($tag_name)."\">$tag_name</A>";
		}
	}

	$request21 = "SELECT tag_parent FROM $trdb->tags WHERE tag_ID =$org_tagid";
	$parent_id = $trdb->get_var($request21);

	if($parent_id!=0){
		//大理上面的'云南'
		$request22 = "SELECT tag_name,tag_nicename FROM $trdb->tags WHERE tag_ID=$parent_id";
		$tags = $trdb->get_results($request22);
		$numbers = count($tags);
		for($i=0;$i<$numbers;$i++)
		{
			$tag = $tags[$i];
			$tag_name = $tag->tag_name;
			$tag_nicename = $tag->tag_nicename;
			if($plan)
				$sText .= " <A href=\"javascript://\" onclick=\"javascript:searchBarNewAdd('$tag_name')\">$tag_name</A>";
			else{
				$sText .= " <A href=\"./?place=".urlencode($tag_name)."\">$tag_name</A>";
			}
		}

		//大理平行的,云南下面的'丽江'
		$request32 = "SELECT tag_name,tag_nicename FROM $trdb->tags WHERE tag_parent=$parent_id AND tag_ID!=$org_tagid LIMIT 5";
		$tags = $trdb->get_results($request32);
		$numbers = count($tags);
		for($i=0;$i<$numbers;$i++)
		{
			$tag = $tags[$i];
			$tag_name = $tag->tag_name;
			$tag_nicename = $tag->tag_nicename;
			
			if($plan)
				$sText .= " <A href=\"javascript://\" onclick=\"javascript:searchBarNewAdd('$tag_name')\">$tag_name</A>";
			else{
				$sText .= " <A href=\"./?place=".urlencode($tag_name)."\">$tag_name</A>";
			}
		}
	}

	if($sText=="")
		$sText = "<暂无>";
	if($echo)
		echo $sText;
	else
		return $sText;
}

/***** optionadmn functions *****/

function the_optionadmn_id($echo = true) {
    global $post;
	$the_optionadmn_id = $post->option_id;
	if ($echo)
    	echo $the_optionadmn_id;
	else
		return $the_optionadmn_id;
}

function the_optionadmn_name($echo = true) {
    global $post;
	$the_optionadmn_name = $post->option_name;
	if ($echo)
    	echo $the_optionadmn_name;
	else
		return $the_optionadmn_name;
}

function the_optionadmn_des($echo = true) {
    global $post;
	$the_optionadmn_des = $post->option_description;
	if ($echo)
    	echo $the_optionadmn_des;
	else
		return $the_optionadmn_des;
}

function the_optionadmn_value($echo = true) {
    global $post;
	$the_optionadmn_value = $post->option_value;
	if ($echo)
    	echo $the_optionadmn_value;
	else
		return $the_optionadmn_value;
}

/***** tagadmn functions *****/

function the_tagadmin_id($echo = true) {
    global $post;
	$the_tagadmin_id = $post->tag_ID;
	if ($echo)
    	echo $the_tagadmin_id;
	else
		return $the_tagadmin_id;
}

function the_tagadmin_name($echo = true) {
    global $post;
	$the_tagadmin_name = $post->tag_name;
	if ($echo)
    	echo $the_tagadmin_name;
	else
		return $the_tagadmin_name;
}

function the_tagadmin_nicename($echo = true) {
    global $post;
	$the_tagadmin_nicename = $post->tag_nicename;
	if ($echo)
    	echo $the_tagadmin_nicename;
	else
		return $the_tagadmin_nicename;
}

function the_tagadmin_description($echo = true) {
    global $post;
	$the_tagadmin_description = $post->tag_description;
	if ($echo)
    	echo $the_tagadmin_description;
	else
		return $the_tagadmin_description;
}

function the_tagadmin_parent($echo = true) {
    global $post;
	$the_tagadmin_parent = $post->tag_parent;
	if ($echo)
    	echo $the_tagadmin_parent;
	else
		return $the_tagadmin_parent;
}

function the_tagadmin_long($echo = true) {
    global $post;
	$the_tagadmin_long = $post->tag_long;
	if ($echo)
    	echo $the_tagadmin_long;
	else
		return $the_tagadmin_long;
}

function the_tagadmin_lati($echo = true) {
    global $post;
	$the_tagadmin_lati = $post->tag_lati;
	if ($echo)
    	echo $the_tagadmin_lati;
	else
		return $the_tagadmin_lati;
}

function the_tagadmin_zoom_in($echo = true) {
    global $post;
	$the_tagadmin_zoom_in = $post->zoom_in;
	if ($echo)
    	echo $the_tagadmin_zoom_in;
	else
		return $the_tagadmin_zoom_in;
}

function the_tagadmin_photo_url($echo = true) {
    global $post;
	$the_tagadmin_photo_url = $post->photo_url;
	//if($the_tagadmin_photo_url=="")
	//	$the_tagadmin_photo_url="http://";
	if ($echo)
    	echo $the_tagadmin_photo_url;
	else
		return $the_tagadmin_photo_url;
}

?>